/*
 * Copyright 2016-2021 Pnoker. All Rights Reserved.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *     http://www.apache.org/licenses/LICENSE-2.0
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.rundow.common.sdk.service.impl;


import com.rundow.common.base.bean.point.PointValue;
import com.rundow.common.base.exception.ServiceException;
import com.rundow.common.base.model.Device;
import com.rundow.common.sdk.bean.driver.ContainerContext;
import com.rundow.common.sdk.bean.driver.DriverContext;
import com.rundow.common.sdk.bean.driver.ServiceContainerContext;
import com.rundow.common.sdk.service.DriverCommandService;
import com.rundow.common.sdk.service.DriverCustomService;
import com.rundow.common.sdk.service.DriverService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Primary;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * @author pnoker
 */
@Slf4j
@Service
@Scope("prototype")
@Primary
public class DriverCommandServiceImpl implements DriverCommandService {


    @Resource
    private ContainerContext containerContext;

    @Autowired
    private ServiceContainerContext serviceContainerContext;
    @Resource
    private DriverService driverService;


    @Override
    public PointValue read(String driverName, String deviceId, String pointId) {
        try {
            final DriverContext driverContext = containerContext.factoryDriverContext(driverName);
            Device device = driverContext.getDeviceByDeviceId(deviceId);
            log.info("driverName={},deviceName={}", driverName, driverName);
            DriverCustomService currentCustomService = serviceContainerContext.getCurrentCustomService(driverName);
            String rawValue = currentCustomService.read(
                    driverContext.getDriverInfoByDeviceId(deviceId),
                    driverContext.getPointInfoByDeviceIdAndPointId(deviceId, pointId),
                    device,
                    driverContext.getPointByDeviceIdAndPointId(deviceId, pointId)
            );
            final String value = driverService.convertValue(driverName,deviceId, pointId, rawValue);
            PointValue pointValue = new PointValue(deviceId, pointId, rawValue, value);
            driverService.pointValueSender(pointValue);
            return pointValue;
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

    @Override
    public Boolean write(String driverName, String deviceId, String pointId, String value) {
        final DriverContext driverContext = containerContext.factoryDriverContext(driverName);
        Device device = driverContext.getDeviceByDeviceId(deviceId);
        try {



            return true;
        } catch (Exception e) {
            throw new ServiceException(e.getMessage());
        }
    }

}
